import { queryListData, addData, updateData, removeData } from '../services/productType';

export default {
  namespace: 'productType',

  state: {
    productType: [],
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(queryListData, payload);
      yield put({
        type: 'handleListData',
        payload: response,
      });
    },
    *get({ payload }, { call, put }) {
      const response = yield call(addData, payload);
      yield put({
        type: 'handleGetData',
        payload: response,
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addData, payload);
      yield put({
        type: 'handleAddData',
        payload: response,
      });
    },
    *update({ payload }, { call, put }) {
      const response = yield call(updateData, payload);
      yield put({
        type: 'handleUpdateData',
        payload: response,
      });
    },
    *remove({ payload }, { call, put }) {
      const response = yield call(removeData, payload);
      yield put({
        type: 'handleRemoveData',
        payload: response,
      });
    },
  },

  reducers: {
    handleListData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          list: [],
          total: 0,
        };
      }

      return {
        ...state,
        data: d,
      };
    },
    handleGetData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          state: -10001,
          message: '接口调用错误',
        };
      }

      return {
        ...state,
        data: d,
      };
    },
    handleAddData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          state: -10001,
          message: '接口调用错误',
        };
      }

      return {
        ...state,
        data: d,
      };
    },
    handleRemoveData(state, action) {
      let d = action.payload;

      if (d === undefined) {
        d = {
          state: -10001,
          message: '接口调用错误',
        };
      }

      return {
        ...state,
        data: d,
      };
    },
  },
};
